package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 通知查询请求DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "通知查询请求参数")
public class NoticeRequest {

    @NotNull(message = "用户ID不能为空")
    @Positive(message = "用户ID必须是正整数")
    @Schema(description = "用户ID", example = "123", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long userId;

    @Positive(message = "通知类型ID必须是正整数")
    @Schema(description = "通知类型ID", example = "1")
    private Long typeId;

    @NotBlank(message = "标签类型不能为空")
    @Schema(description = "标签类型", example = "received", allowableValues = {"received", "published"}, requiredMode = Schema.RequiredMode.REQUIRED)
    private String tabType;

    @NotNull(message = "页码不能为空")
    @Positive(message = "页码必须大于0")
    @Schema(description = "页码（从1开始）", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer page;

    @NotNull(message = "每页大小不能为空")
    @Positive(message = "每页大小必须大于0")
    @Schema(description = "每页记录数", example = "20", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer size;

    @Schema(description = "阅读状态", example = "unread", allowableValues = {"read", "unread"})
    private String readStatus;

    @Schema(description = "关键词搜索")
    private String keyword;

    @Schema(description = "开始时间（格式：yyyy-MM-dd）")
    private String startDate;

    @Schema(description = "结束时间（格式：yyyy-MM-dd）")
    private String endDate;

    @Schema(description = "是否包含已过期通知", example = "false")
    private Boolean includeExpired;

    /**
     * 获取偏移量（用于数据库查询）
     */
    @Schema(description = "查询偏移量", accessMode = Schema.AccessMode.READ_ONLY)
    public Integer getOffset() {
        return (page - 1) * size;
    }

    /**
     * 验证标签类型是否有效
     */
    @Schema(description = "标签类型是否有效", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isValidTabType() {
        return "received".equals(tabType) || "published".equals(tabType);
    }

    /**
     * 验证阅读状态是否有效
     */
    @Schema(description = "阅读状态是否有效", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isValidReadStatus() {
        return readStatus == null || "read".equals(readStatus) || "unread".equals(readStatus);
    }

    /**
     * 是否需要查询接收的通知
     */
    @Schema(description = "是否查询接收的通知", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isQueryReceived() {
        return "received".equals(tabType);
    }

    /**
     * 是否需要查询发送的通知
     */
    @Schema(description = "是否查询发送的通知", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isQueryPublished() {
        return "published".equals(tabType);
    }

    /**
     * 构建默认查询请求
     */
    public static NoticeRequest defaultRequest(Long userId) {
        return NoticeRequest.builder()
                .userId(userId)
                .tabType("received")
                .page(1)
                .size(20)
                .includeExpired(false)
                .build();
    }
}
